<?php

namespace app\web\controller;

use think\exception\ValidateException;
use app\commons\model\SaleVerifyer;
use redis\Redis;
use think\facade\Log;

class Verifyer
{
    /**
     * 绑定核销员
     */
    public function create(SaleVerifyer $saleVerifyer){
        $data = request()->param();
        if(!isset($data['code'])) throw new ValidateException('非法操作！');
        $sale_id = Redis::redis()->get('douyin:sale:code:'.$data['code']);
        if(!$sale_id){
            Log::info("没有获取到：");
            Log::info($data);
            throw new ValidateException('操作已超时，请重新扫码！');
        }
        $saleVerifyerRes = $saleVerifyer->allowField(true)->save(['member_id'=>session('user'),'sale_id'=>$sale_id]);
        if(!$saleVerifyerRes) throw new ValidateException('绑定失败！');
        Redis::redis()->del('code:'.$data['code']);//删除绑定code
        return jsonp();
    }
}